<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <div class="app-space-between">
        <div class="app-space-between-left">
          <el-form-item label="商品分类" prop="goodsType">
            <div style="width: 200px; font-size:13px;height:32px;">
              <treeselect v-model="queryParams.goodsType" :options="[{goodsTypeId:'',goodsTypeName:'根目录'},...dataTable]"
                :normalizer="normalizer" :show-count="true" placeholder="选择上级菜单" />
            </div>
          </el-form-item>
          <el-form-item label="商品名称" prop="goodsName">
            <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @clear="handleQuery"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </div>
        <div class="app-space-between-right">
          <el-form-item>
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="分类名称" align="center" prop="goodsType" />
      <el-table-column label="商品价格(元)" align="center" prop="sellingPrice" />
      <el-table-column label="规格单位" align="center" prop="standards" />
      <el-table-column label="当前库存量" align="center" prop="inventory" />
      <el-table-column label="预警库存量" align="center" prop="warningQuantity" />

      <el-table-column label="商品状态" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div>{{scope.row.isOnshow}}</div>
          <el-button size="mini" type="text" @click="handleEvaluate(scope.row)">查看评论</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
      <!-- 商品选择 -->
      <com-add ref="com-add" @success="getList" :dataTable="dataTable" :dataOptions="dataOptions"></com-add>
      <!-- 评论 -->
      <com-evaluate ref="com-evaluate" />
  </div>
</template>

<script>
  import {
    storeGoodsList,
    storeGoodsDelete
  } from "@/api/store/content";
  import {
    goodsTypeList,
  } from "@/api/shopping/column"
  import Treeselect from "@riophae/vue-treeselect"
  import "@riophae/vue-treeselect/dist/vue-treeselect.css"
  import ComAdd from './add'
  import ComEvaluate from '@/views/shopping/content/components/Evaluate'
  export default {
    components:{
      Treeselect,
      ComAdd,
      ComEvaluate
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          goodsType: '',
          goodsName: '',
        },
        // 表单参数
        form: {},
        dataTable:[],
        dataOptions:[],
        // 表单校验
        rules: {
          postName: [{
            required: true,
            message: "岗位名称不能为空",
            trigger: "blur"
          }],
          postCode: [{
            required: true,
            message: "岗位编码不能为空",
            trigger: "blur"
          }],
          postSort: [{
            required: true,
            message: "岗位顺序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    watch:{
      'queryParams.goodsType'(){
        this.getList()
      }
    },
    created() {
      this.getList();
      goodsTypeList({}).then(response => {
        this.dataOptions = response.rows
        this.dataTable = this.handleTree(response.rows, 'goodsTypeId', 'goodsTypeParentid')
      })
    },
    methods: {
      handleEvaluate(options){
        this.$refs['com-evaluate'].init(options)
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.goodsTypeId,
          label: node.goodsTypeName,
          children: node.children
        };
      },
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        storeGoodsList({
          ...this.queryParams,
          storeId:this.$route.params.id
        }).then(response => {
          console.log('商品列表', response)
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleDelete(options){
        this.$modal.confirm('此操作将要删除所选数据，是否继续？').then(function() {
          return storeGoodsDelete({
            storeId:options.storeId,
            goodsId:options.goodsId
          });
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.postId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs['com-add'].init('add')
      }
    }
  };
</script>
<style lang="scss" scoped>
  ::v-deep .vue-treeselect__control{
    height: 32px;
    display: block;
    table-layout: none;
    padding-right: 10px !important;
    box-sizing:border-box !important;
  }
  ::v-deep .vue-treeselect__placeholder, .vue-treeselect__single-value{
    line-height: 32px !important;
  }
  ::v-deep .vue-treeselect__x-container{
    padding-right: 15px !important;
  }
</style>
